﻿@using System.Web.Mvc
@using Whoever.Framework.Utils

@helper MenuNav(string defaultName, string[] categories, params string[][] controllerNames)
{
    var allControllerNames = new List<string>();

    foreach (var controllerName in controllerNames)
    {
        allControllerNames.AddRange(controllerName);
    }

    <li @Dropdown.ActiveBinding(allControllerNames.ToArray())>
        <a class="dropdown-toggle" data-toggle="dropdown">
            <span @Dropdown.TextBinding(defaultName, allControllerNames.ToArray())></span>
            <span class='caret'></span>
        </a>
        <ul class="dropdown-menu" role="menu">

            @{
                int i = 0;

                foreach (var category in categories)
                {
                    <text>
                        <li><span class="category">@category</span></li>
                    </text>
                    var categoryControllerNames = controllerNames[i];
                    foreach (var categoryControllerName in categoryControllerNames)
                    {
                        <text>
                            @Dropdown.NavLink(categoryControllerName)
                        </text>
                    }
                    i++;
                }
            }
        </ul>
    </li>
}

@helper NavLink(string controllerName)
{
    var urlBrowser = new UrlHelper(Request.RequestContext);

    <li><a href="#" data-bind="click: Main.Page.ChangePage.bind($data,'@urlBrowser.Action("Main", controllerName)')">@Localized.Action(string.Format("Nav.{0}", controllerName))</a></li>
}

@helper ActiveBinding(params string[] controllerNames)
{
    var condition = string.Empty;

    foreach (var controllerName in controllerNames)
    {
        condition += string.Format(" Main.State.CurrentController() == '{0}' ||", controllerName);
    }

    condition = condition.Remove(condition.Length - 2, 2);

    <text>
        data-bind="css: { active: @condition }"
    </text>
}

@helper TextBinding(string defaultName, string[] controllerNames)
{
    var condition = string.Empty;

    foreach (var controllerName in controllerNames)
    {
        condition += string.Format(" Main.State.CurrentController() == '{0}' ||", controllerName);
    }

    condition = condition.Remove(condition.Length - 2, 2);

    <text>
        data-bind='text: @condition ? Main.State.CurrentController()  : "@defaultName"'
    </text>
}